package com.mes.web.controller.cb;

import com.mes.service.cb.SubmiTaskService;
import com.mes.utils.ResponseUtil;
import net.sf.json.JSONObject;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: SubmiTaskController
 * @Description: 提交任务
 * @Author cb
 * @Date 2018年3月29日15:32:55
 * @version 1.0
 */
@Controller
@RequestMapping("submit")
public class SubmiTaskController {


    @Resource
    private TaskService taskService;

    @Resource
    private RuntimeService runtimeService;

    @Resource
    private SubmiTaskService submiTaskService;

    @RequestMapping("/startApply")
    public void startApply(String rid,  HttpServletResponse response) throws Exception {


        //启动流程时例
        /*Map<String, Object> variables = new HashMap<>();

        variables.put("retuenManager","王敏");

        variables.put("returnFman","欧式,赵六");

        variables.put("returnGman","闽南");


        ProcessInstance pi = runtimeService.startProcessInstanceByKey("myMaReturn", variables);
        Task task = taskService.createTaskQuery().processInstanceId(pi.getProcessInstanceId()).singleResult();
        //审批完成
        taskService.complete(task.getId());*/

        //将"未审批"改为审批中

        Map<String, Object> map = new HashMap<>();
        map.put("rid", rid);
        map.put("state", "2");
        map.put("prremark2", "");

        int total = submiTaskService.returenMaterie_updateByState(map);
        JSONObject jsonObject = new JSONObject();
        if (total > 0) {
            jsonObject.put("success", true);
        } else {
            jsonObject.put("success", false);
        }

        ResponseUtil.write(response, jsonObject);
    }


    /**
     * 计划单
     * @param pid
     * @param response
     * @throws Exception
     */
    @RequestMapping("/planStartApply")
    public void planStartApply(String pid,  HttpServletResponse response) throws Exception {


        //启动流程时例
        /*Map<String, Object> variables = new HashMap<>();

        variables.put("planManager","王敏");

        variables.put("planFman","欧式,赵六");

        variables.put("myGman","闽南");


        ProcessInstance pi = runtimeService.startProcessInstanceByKey("myPlan", variables);
        Task task = taskService.createTaskQuery().processInstanceId(pi.getProcessInstanceId()).singleResult();
        //审批完成
        taskService.complete(task.getId());*/

        //将"未审批"改为审批中

        Map<String, Object> map = new HashMap<>();
        map.put("rid", pid);
        map.put("state", "2");
        map.put("prremark2","");

        int total = submiTaskService.planMaterie_updateByState(map);
        JSONObject jsonObject = new JSONObject();
        if (total > 0) {
            jsonObject.put("success", true);
        } else {
            jsonObject.put("success", false);
        }

        ResponseUtil.write(response, jsonObject);
    }
}
